﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Core.RecordSiteQuery;
using MS.InformationManagement.DAL.RecordCenter;
using MS.InformationManagement.DAL.Support;
using System.Data;
using Microsoft.SharePoint.Taxonomy;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.BLL.TomoManagement
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class TomoManagement
    {
        public Tomo GetVolumeById(int volumeId)
        {
            Tomo founded = null;
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                founded = (from rdc in dc.GetList<Tomo>(GlobalConstants.LIST_TOMOS)
                           where rdc.Id == volumeId
                           select rdc).FirstOrDefault();
            }

            return founded;
        }

        public List<Tomo> GetListVolumeByExpedient(string expedienteId)
        {
            List<Tomo> founded = new List<Tomo>();
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                founded = (from rdc in dc.GetList<Tomo>(GlobalConstants.LIST_TOMOS)
                           where rdc.NúmeroExpediente == expedienteId
                           select rdc).ToList();
            }
             
            return founded;
        }

        public void UpdateVolume(Tomo volume)
        {
            Core.TomoActions.TomoActions coreTomo = new Core.TomoActions.TomoActions();
            coreTomo.CurrentVolumeEntity = volume;
            coreTomo.UpdateVolume((int)volume.Id);

            //using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            //{
            //    var entity = (from exp in dc.GetList<Tomo>(GlobalConstants.LIST_TOMOS)
            //                  where exp.Id == volume.Id
            //                  select exp).FirstOrDefault();
            //    entity.Caja = volume.Caja;
            //    entity.Cara = volume.Cara;
            //    entity.Entrepaño = volume.Entrepaño;
            //    entity.Estante = volume.Estante;
            //    entity.Piso = volume.Piso;
            //    entity.FechaFinal = DateTime.Now;
            //    dc.SubmitChanges();
            //}
        }

        //public void UpdateLastDateInVolume(Tomo volume)
        //{
        //    using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
        //    {
        //        var entity = (from exp in dc.GetList<Tomo>(GlobalConstants.LIST_TOMOS)
        //                      where exp.Id == volume.Id
        //                      select exp).FirstOrDefault();
        //        entity.FechaFinal = DateTime.Now;
        //        dc.SubmitChanges();
        //    }
        //}

        //public void UpdateLastDateInVolume(Tomo volume, SPWeb web)
        //{
        //    SPList targetList = web.Lists.TryGetList(GlobalConstants.LIST_TOMOS);
        //    if (targetList == null) { throw new Exception("La lista de tomos no existe"); }
        //    SPListItem tome = targetList.GetItemById(volume.Id.Value);
        //    if (tome == null) { throw new Exception("El tomo no existe"); }
        //    web.AllowUnsafeUpdates = true;
        //    tome[CustomSiteColumns.FIELD_VOLUME_FINAL_DATE] = DateTime.Now;
        //    tome.Update();
        //    web.AllowUnsafeUpdates = false;
        //}

        public void CreateVolume(string expedienteId)
        {
            // Obtiene el ultimo tomo creado para el expediente
            Tomo founded = null;
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                founded = (from rdc in dc.GetList<Tomo>(GlobalConstants.LIST_TOMOS)
                           where rdc.NúmeroExpediente == expedienteId
                           select rdc).OrderByDescending(r => r.FechaInicial).FirstOrDefault();
            }

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        SPList userList = currentWeb.Lists.TryGetList(GlobalConstants.LIST_TOMOS);
                        currentWeb.AllowUnsafeUpdates = true;
                        SPListItem newItem = userList.Items.Add();

                        newItem[SPBuiltInFieldId.Title] = (int.Parse(founded.Title) + 1).ToString();
                        newItem[CustomSiteColumns.FIELD_VOLUME_INITIAL_DATE] = DateTime.Now;
                        newItem[CustomSiteColumns.FIELD_VOLUME_FINAL_DATE] = DateTime.Now;
                        newItem[CustomSiteColumns.FIELD_DOCUMENT_EXPEDIENTE_ID] = expedienteId;
                        newItem.Update();

                        currentWeb.AllowUnsafeUpdates = false;
                    }
                }
            });
        }
    }
}
